//
//  DGGlobalTimerManager.m
//  DGGlobalTimer
//
//  Created by Jinxiao on 7/24/14.
//  Copyright (c) 2014 debugeek. All rights reserved.
//

#import "DGGlobalTimerManager.h"
#import "DGGlobalTimerTask.h"
#import "DGGlobalTimerTaskManager.h"
#import "DGGlobalTimerObserverManager.h"
#import "DGGlobalTimerObserver.h"

@implementation DGGlobalTimerManager

+ (void)load
{
    [[DGGlobalTimerTaskManager sharedInstance] restoreAllTimerTasks];
}

+ (instancetype)sharedInstance
{
    static id instance;
    static dispatch_once_t once;
    dispatch_once(&once, ^{
        instance = self.new;
    });
    return instance;
}

- (void)rescheduleGlobalTimerForTaskKey:(NSString *)taskKey interval:(NSTimeInterval)interval duration:(NSTimeInterval)duration
{
    DGGlobalTimerTask *task = [[DGGlobalTimerTaskManager sharedInstance] timerTaskForKey:taskKey];
    [task drop];
    
    task = [[DGGlobalTimerTask alloc] init];
    task.key = taskKey;
    task.duration = duration;
    task.interval = interval;
    task.fired = [[NSDate date] timeIntervalSince1970];
    [task schedule];
    
    [[DGGlobalTimerTaskManager sharedInstance] addTimerTask:task];
}

- (void)scheduleGlobalTimerForTaskKey:(NSString *)taskKey interval:(NSTimeInterval)interval duration:(NSTimeInterval)duration
{
    DGGlobalTimerTask *task = [[DGGlobalTimerTaskManager sharedInstance] timerTaskForKey:taskKey];
    if(!task)
    {
        task = [[DGGlobalTimerTask alloc] init];
        task.key = taskKey;
        task.duration = duration;
        task.interval = interval;
        task.fired = [[NSDate date] timeIntervalSince1970];
        [task schedule];
        
        [[DGGlobalTimerTaskManager sharedInstance] addTimerTask:task];
    }
}

- (void)removeGlobalTimerForTaskKey:(NSString *)taskKey
{
    DGGlobalTimerTask *task = [[DGGlobalTimerTaskManager sharedInstance] timerTaskForKey:taskKey];
    if(task)
    {
        [task dropForced:YES];
    }
}

- (void)observeGlobalTimerForIdentifier:(NSString *)identifier
                                taskKey:(NSString *)taskKey
                          preparedBlock:(void (^)())preparedBlock
                       initializedBlock:(void (^)(NSTimeInterval remains))initializedBlock
                         triggeredBlock:(void (^)(NSTimeInterval remains))triggeredBlock
                         completedBlock:(void (^)(void))completedBlock
{
    DGGlobalTimerObserver *observer = [[DGGlobalTimerObserverManager sharedInstance] observerForIdentifier:identifier];
    if(observer)
    {
        [[DGGlobalTimerObserverManager sharedInstance] removeGlobalTimerObserver:observer];
    }
    
    observer = [[DGGlobalTimerObserver alloc] init];
    observer.key = taskKey;
    observer.identifier = identifier;
    observer.preparedBlock = preparedBlock;
    observer.initializedBlock = initializedBlock;
    observer.triggeredBlock = triggeredBlock;
    observer.completedBlock = completedBlock;
    [[DGGlobalTimerObserverManager sharedInstance] addGlobalTimerObserver:observer];
}

@end
